package com.hqyj.springboot.modules.account.dao;

import com.hqyj.springboot.modules.account.entity.User;
import com.hqyj.springboot.modules.common.vo.Search;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import javax.persistence.Column;
import java.util.List;


@Repository
@Mapper
public interface UserDao2 {

    @Insert("insert into test_user (create_date,update_date,user_name,password)" +
            " values (#{createDate},#{updateDate},#{userName},#{password})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void addUser(User user);

    @Select("select * from test_user where user_name = #{userName}")
    List<User> getUserByUserName(String userName);

    @Select("select * from test_user where user_name=#{userName} and password=#{password}")
    User getUserByUserNameAndPassword(@Param("userName") String userName,@Param("password") String password);

    @Update("update test_user set update_date = #{updateDate} and user_name = #{userName} and password = #{password} where id = #{id}")
    void updateUser(User user);

    @Update("update test_user set password = #{password} where id = #{id}")
    void updateUserPassword(@Param("password") String password,@Param("id") int id);

    @Select("select * from test_user where id = #{id}")
    @Results(id = "userResult",value = {
            @Result(column = "id",property = "id"),
            @Result(column = "id",property = "roles",javaType = List.class,
            many = @Many(select = "com.hqyj.springboot.modules.account.dao.RoleDao2.getRolesByUserId"))

    })
    User getUserAndUserRoleById(int id);

    @Delete("delete from test_user where id = #{id}")
    void deleteUserById(int id);

    @Select("<script>"
            + "select * from test_user "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and ( user_name like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='sort != \"\" and sort != null'>"
            + " order by ${sort} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<User> getUsersBySearchVo(Search search);





}
